package model

import (
	"fmt"
	"gorm.io/gorm"
)

func AddJob(db *gorm.DB, value *Job) error {
	if value.SampleID == "" && value.SelectionID == 0 {
		return fmt.Errorf("missing sample_id or selection_id")
	}

	return db.Create(value).Error
}

func ListSampleJob(db *gorm.DB, authorId uint) ([]Sample, error) {
	var ret1 []Job

	builder := db.Where("author_id = ?", authorId)

	if err := builder.Find(&ret1).Error; err != nil {
		return nil, err
	}

	var ret []Sample
	for _, value := range ret1 {
		if value.SampleID != "" {
			var temp Sample
			db.Where("id = ?", value.SampleID).First(&temp)
			if temp.ID != "" {
				ret = append(ret, temp)
			}
		}

	}

	return ret, nil
}

func ListSelectionJob(db *gorm.DB, authorId uint) ([]Selection, error) {
	var ret1 []Job

	builder := db.Where("author_id = ?", authorId)

	if err := builder.Find(&ret1).Error; err != nil {
		return nil, err
	}

	var ret []Selection
	for _, value := range ret1 {
		if value.SelectionID != 0 {
			var temp Selection
			db.Where("id = ?", value.SelectionID).First(&temp)
			if temp.ID != 0 {
				ret = append(ret, temp)
			}
		}

	}

	return ret, nil
}
